extends PanelContainer

@export var scheduleData:ScheduleData

@onready var time_label = %time_label
@onready var progressBar = %TextureProgressBar
@onready var action_name = %action_name
@onready var ability_name_scene = preload("res://components/ability_name.tscn")
@onready var ability_name_container = %ability_name_container
@onready var timer = $Timer

@onready var startBtn = %start_btn

var is_timing = false
var progress = 0
var total_seconds = 0

# Called when the node enters the scene tree for the first time.
func _ready():
	if scheduleData!=null:
		update_data()

func update_data():
	var state = GlobalVariables.check_schedule_state(scheduleData)
	if state == GlobalVariables.ScheduleState.FINISHED:
		var title = '日程已完成！\n计划%02d分钟\n实际用时%02d分钟' % [scheduleData.length, float(scheduleData.actual_length)/60]
		set_disabled_state(title)
		
	elif state == GlobalVariables.ScheduleState.UNFINISHED:
		var title = '过期未处理\n已标记为未完成!\n请继续努力!'
		set_disabled_state(title)

	elif state == GlobalVariables.ScheduleState.DELAYED:
		var title = '已推迟到明天!\n请注意合理规划!'
		set_disabled_state(title)
	elif state == GlobalVariables.ScheduleState.UNSTARTED:
		set_disabled_state('未开始！')
	else:
		progress = scheduleData.progress
		total_seconds = scheduleData.length*60
		
		set_time_label()
		action_name.text = scheduleData.schedule_title
		set_ability_labels()

func set_disabled_state(title:String):
	time_label.text = title
	time_label.add_theme_font_size_override("font_size",50)
	time_label.add_theme_color_override("font_color",Color.PALE_VIOLET_RED)
	#%functionZone.hide()
	%start_btn.disabled = true
	%add_time.disabled = true
	%finish_btn.disabled = true
	%delay_btn.disabled = true

func update_time_label():
	progress+=1
	set_time_label()
	scheduleData.progress = progress
	GlobalVariables.save_schedule(scheduleData,false)
	progressBar.value = int((1-float(progress)/total_seconds)*100)
	
	if total_seconds - progress <= 0:
		timer.stop()
		startBtn.disabled = true
		
		# 播放提示音乐
		%AudioStreamPlayer.play()
		scheduleData.is_finished = true
		scheduleData.actual_length = progress
		GlobalVariables.save_schedule(scheduleData)
		

func set_time_label():
	var last_time = total_seconds - progress
	last_time = 0 if last_time <0 else last_time
	time_label.text = "%02d:%02d" %[int(last_time/float(60)), last_time%60]
		
	
func set_ability_labels():
	var related_ability = GlobalVariables.get_ability(GlobalVariables.load_task(scheduleData.related_task).related_ability)
	for ability in GlobalVariables.get_ability_parents(related_ability):
		var ability_label = ability_name_scene.instantiate()
		ability_label.abilityEn = ability
		ability_name_container.add_child(ability_label)

func _on_start_btn_pressed():
	
	if not is_timing:
		# 开始倒计时
		timer.start()
		startBtn.text = '暂停'
	else:
		timer.stop()
		startBtn.text = '开始'
	
	is_timing = !is_timing

func _on_timer_timeout():
	update_time_label()


func _on_add_time_pressed():
	total_seconds += 10*60
	set_time_label()


func _on_finish_btn_pressed():
	timer.stop()
	scheduleData.is_finished = true
	scheduleData.actual_length = progress
	GlobalVariables.save_schedule(scheduleData)
	#GlobalUtil.check_task_process(GlobalVariables.load_task(scheduleData.related_task),true)
	Signalbus.schedule_finished.emit(scheduleData)
	page_back()
	
	
func page_back():
	if GlobalVariables.action_history.size()>0:
		var preActionData = GlobalVariables.action_history.pop_back()
		Signalbus.action_back_requested.emit(preActionData)

func _on_delay_btn_pressed():
	scheduleData.is_delayed = true
	GlobalVariables.save_schedule(scheduleData)
	Signalbus.schedule_arrange_request.emit(scheduleData.related_task)
	page_back()



